package com.atguigu.tingshu.search.factory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Description:
 * 定时任务线程工程类
 * @author Yanxinhao on 2025/2/9
 */
public class ScheduleTaskThreadFactory {

    private ScheduleTaskThreadFactory(){

    }

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);


    private static ScheduleTaskThreadFactory INSTANCE = new ScheduleTaskThreadFactory();


    public static ScheduleTaskThreadFactory getInstance(){
        return INSTANCE;
    }

    public void execute(Runnable runnable, long ttl, TimeUnit timeUnit){
        scheduledExecutorService.schedule(runnable,ttl,timeUnit);
    }


    /**
     * 时间差
     *
     * @param currentTime
     * @return
     */
    public long diffTime(Long currentTime) {
        LocalDate localDate = LocalDate.now().plusDays(7);
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(2, 0, 0));

        long endTime = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long diffTime = Math.abs(currentTime - endTime);
        return diffTime;
    }
}